<?php

global $config;

$op = @ $_REQUEST["op"];

define("MODE_ADMIN", 1);
define("MODE_LOJA", 2);
define("NO_MODE", 23);

$functions = array(
    
                    "add_loca" => array("control" => "localidade", "function" => "addLocalidade", "mode" => MODE_LOJA) ,
                    "del_loca" => array("control" => "localidade", "function" => "removerLocalidade", "mode" => MODE_LOJA),
                    
                    "add_regi" => array("control" => "regiao", "function" => "addRegiao", "mode" => MODE_LOJA ),
                    "del_regi" => array("control" => "regiao", "function" => "removerRegiao", "mode" => MODE_LOJA),
    
                    "add_rota" => array("control" => "rota", "function" => "addRota", "mode" => MODE_LOJA),
                    "del_rota" => array("control" => "rota", "function" => "removeRota", "mode" => MODE_LOJA),
                    
                    "add_loja" => array("control" => "loja", "function" => "addLoja", "mode" => MODE_ADMIN),
                    "del_loja" => array("control" => "loja", "function" => "removeLoja", "mode" => MODE_ADMIN),
   
                    "add_func" => array("control" => "funcionario", "function" => "addFuncionario", "mode" => MODE_ADMIN),
                    "del_func" => array("control" => "funcionario", "function" => "removeFuncionario", "mode" => MODE_ADMIN),  
    
                    "add_clie" => array("control" => "cliente", "function" => "addCliente", "mode" => NO_MODE),
                    "del_clie" => array("control" => "cliente", "function" => "removeCliente", "mode" => NO_MODE),  
                    
                    "add_labo" => array("control" => "laboratorio", "function" => "addLaboratorio", "mode" => MODE_ADMIN),
                    "del_labo" => array("control" => "laboratorio", "function" => "removerLaboratorio", "mode" => MODE_ADMIN),    
                        
                    "add_prod" => array("control" => "produto", "function" => "addProduto", "mode" => MODE_ADMIN),
                    "del_prod" => array("control" => "produto", "function" => "removerProduto", "mode" => MODE_ADMIN),
                    
                    "add_tipo" => array("control" => "produto", "function" => "addTipoProduto", "mode" => MODE_ADMIN),
                    "del_tipo" => array("control" => "produto", "function" => "removerTipoProduto", "mode" => MODE_ADMIN),
                    
                    "add_marc" => array("control" => "produto", "function" => "addMarca", "mode" => MODE_ADMIN),
                    "del_marc" => array("control" => "produto", "function" => "removerMarca", "mode" => MODE_ADMIN),
    
                    "add_orde" => array("control" => "ordemServico", "function" => "addOS", "mode" => NO_MODE),
                    "del_orde" => array("control" => "ordemServico", "function" => "removeOS", "mode" => MODE_ADMIN),    
    
                   );

if(!empty($functions[$op])) {
    $function = (object) $functions[$op];
    
    switch ($function->mode){
        case MODE_ADMIN: 
            if($_SESSION[SESSION_PERFIL_FUNC] != PERFIL_ADMINISTRADOR){
                $config->failInFunction("Você não tem permissões para essa função");
                $config->redirect("index.php");
            }
            break;
        case MODE_LOJA:
            if($_SESSION[SESSION_PERFIL_FUNC] != PERFIL_ADMINISTRADOR){
                $loja = $config->filter("loja");
                $loja_sigla_func = $_SESSION[SESSION_LOJA_SIGLA_FUNC];
                $loja_id_func = $_SESSION[SESSION_LOJA_FUNC];
                if( strcmp($loja, $loja_id_func) && strcmp($loja, $loja_sigla_func) ) {
                    $config->failInFunction("Essa função está restrita somente a sua loja");
                    $config->redirect("index.php");
                }
            }
            break;
    }
    
    $config->loadCurrentController($function->control);
    $controller = $config->currentController;
    $func_name = $function->function;
    $controller->$func_name();
}
                    //ID            //FORM                      //LIST              //CONTROLLER
$valids = array(    //MÓDULO 1
                    "cad-clie" => array(FORMS."cad-clie.php", LISTS."cad-clie.php", "cliente"),
                    "cad-prod" => array(FORMS."cad-prod.php", LISTS."cad-prod.php", "produto"),
                    "cad-func" => array(FORMS."cad-func.php", LISTS."cad-func.php", "funcionario"),
                    "cad-labo" => array(FORMS."cad-labo.php", LISTS."cad-labo.php", "laboratorio"),
                    "cad-loja" => array(FORMS."cad-loja.php", LISTS."cad-loja.php", "loja"),
                    "cad-regi" => array(FORMS."cad-regi.php", LISTS."cad-regi.php", "regiao"),
                    "cad-loca" => array(FORMS."cad-loca.php", LISTS."cad-loca.php", "localidade"),
                    "cad-rota" => array(FORMS."cad-rota.php", LISTS."cad-rota.php", "rota"),
                    "cad-marc" => array(FORMS."cad-marc.php", LISTS."cad-marc.php", "produto"),
                    "cad-tipo" => array(FORMS."cad-tipo.php", LISTS."cad-tipo.php", "produto"),
                    //MÓDULO 2
                    "cad-orde" => array(FORMS."cad-orde.php", LISTS."cad-orde.php", "ordemServico"), 
                    "cad-vend" => array(FORMS."cad-vend.php"),
		);

define("FORM_IDX", 0);
define("LIST_IDX", 1);
define("CONTROLLER_IDX", 2);

if(!empty($valids[$op])){
    if(isset($valids[$op][CONTROLLER_IDX]))
        $config->loadCurrentController($valids[$op][CONTROLLER_IDX]);
    if(isset($valids[$op][FORM_IDX]) && file_exists($valids[$op][FORM_IDX])) {
        echo "<div class='mini-content'>";
            include_once $valids[$op][FORM_IDX];
        echo "</div><br/>";
    }
    if(isset($valids[$op][LIST_IDX]) && file_exists($valids[$op][LIST_IDX])){
        echo "<div class='mini-content list-mini-content' >";
            include_once $valids[$op][LIST_IDX];
        echo "</div><br/>";
    }
} else {
    include_once HTML."main-toolbar.html";
}
?>
